//
//  SDPhotoCustomTagCell.m
//  SKinDiary
//
//  Created by pcyang on 16/7/16.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPhotoCustomTagCell.h"

#define kTagLeftPadding 10
#define kTopPadding 10

@interface SDPhotoCustomTagCell()<PhotoTagViewDelegate>
@property(nonatomic,strong)SDPhotoTagView* tagItemView1;
@property(nonatomic,strong)SDPhotoTagView* tagItemView2;

@end


@implementation SDPhotoCustomTagCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kTopPadding, 50, 13)];
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.textColor = [UIColor colorWithRGB:0xb4bec8];
        _titleLabel.textAlignment = NSTextAlignmentRight;
        _titleLabel.text = @"自定义标签";
        [_titleLabel sizeToFit];
        [self.contentView addSubview:_titleLabel];
        
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addButton setBackgroundColor:[UIColor colorWithRGB:0xffffff]];
        [_addButton setTitle:@"添加" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor colorWithRGB:0xdddd24d9c6] forState:UIControlStateNormal];
        _addButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _addButton.frame = CGRectMake(0, kTopPadding/2, 55, 25);
        _addButton.layer.cornerRadius = 14;
       CALayer* layer = [_addButton layer];
        layer.borderColor = [UIColor colorWithRGB:0x24d9c6].CGColor;
        layer.borderWidth = 1.0f;
        [self addSubview:_addButton];
        
        _bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(kTagLeftPadding, kPhotoCustomTagCellHeight - 1, kScreenWidth - 2*kTagLeftPadding, 1)];
        [_bottomLineView setBackgroundColor:[UIColor colorWithRGB:0xcad6e3]];
        [self.contentView addSubview:_bottomLineView];
        
        _tagItemsArray = [NSMutableArray arrayWithCapacity:2];
        
        _tagItemView1 = [SDPhotoTagView buttonWithType:UIButtonTypeCustom];
        _tagItemView1.deleteImageview.hidden = NO;
        [_tagItemView1 setBackgroundColor:[UIColor colorWithRGB:0x24d9c6]];
        [_tagItemView1 setTitleColor:[UIColor colorWithRGB:0xffffff] forState:UIControlStateNormal];
        _tagItemView1.delegate = self;
        [self.contentView addSubview:_tagItemView1];
        
        _tagItemView2 = [SDPhotoTagView buttonWithType:UIButtonTypeCustom];
        _tagItemView2.deleteImageview.hidden = NO;
        [_tagItemView2 setBackgroundColor:[UIColor colorWithRGB:0x24d9c6]];
        [_tagItemView2 setTitleColor:[UIColor colorWithRGB:0xffffff] forState:UIControlStateNormal];
        _tagItemView2.delegate = self;
        [self.contentView addSubview:_tagItemView2];
    }
    return self;
}

-(void)layoutSubviews
{
    _titleLabel.right = 75;
    _addButton.right = kScreenWidth - 20;
    
    _tagItemView1.top = _addButton.top;
    _tagItemView1.left = _titleLabel.right + kTagLeftPadding;
    
    _tagItemView2.top = _tagItemView1.bottom + kTagLeftPadding;
    _tagItemView2.left = _tagItemView1.left;
    
}

-(void)setCellData:(id)data
{
    [_tagItemsArray removeAllObjects];
    NSArray* tagList = [data componentsSeparatedByString:NSLocalizedString(@",", nil)];
    for (NSString* tagName in tagList) {
        if (CHECK_VALID_STRING(tagName)) {
            PhotoTagItem* tagItem = [[PhotoTagItem alloc] init];
            tagItem.title = tagName;
            tagItem.inUsed = YES;
            [_tagItemsArray addObject:tagItem];
        }
    }
    [self refreshView];
}

-(void)refreshView
{
    _tagItemView1.hidden = YES;
    _tagItemView2.hidden = YES;
    for (int i = 0 ; i <  _tagItemsArray.count; i++) {
        PhotoTagItem* tagItem = [_tagItemsArray objectAtIndex:i];
        if (i == 0) {
            _tagItemView1.hidden = NO;
            [_tagItemView1 setTagItem:tagItem];
        } else if(i == 1){
            _tagItemView2.hidden = NO;
            [_tagItemView2 setTagItem:tagItem];
        }
    }
    
    [self setNeedsLayout];
}

-(void)addTagItem:(PhotoTagItem*)tagItem
{
    if ([_tagItemsArray containsObject:tagItem]) {
        return;
    }
    
    [_tagItemsArray addObject:tagItem];
    [self refreshView];
}

-(NSArray*)getSelectedTagItems
{
    NSMutableArray* tagItems = [NSMutableArray arrayWithCapacity:0];
    for (PhotoTagItem* tagItem in self.tagItemsArray) {
        [tagItems addObject:tagItem.title];
    }
    
    return tagItems;
}

#pragma mark- PhotoTagViewDelegate
-(void)tagViewPressed:(SDPhotoTagView*)addTagView
{
    PhotoTagItem* tagItem = addTagView.getTagItem;
    if ([_tagItemsArray containsObject:tagItem]) {
        [_tagItemsArray removeObject:tagItem];
        [self refreshView];
    }
}

@end
